package com.shenbianyun.bosskgpay.service.sign;


import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.entity.Merchant;
import com.shenbianyun.bosskgpay.entity.Person;
import com.shenbianyun.bosskgpay.entity.RequestMessage;
import com.shenbianyun.bosskgpay.entity.sign.UserSignReqModel;
import com.shenbianyun.bosskgpay.utils.*;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;


/**
 * @Author yvan
 * @Date 2021/11/3 11:02:39
 * @Description //Jmeter测试环境 自由职业者签约 请求
 **/

public class TestUserSignServiceJ extends AbstractJavaSamplerClient {


    public final String CHARSET_UTF_8 = "UTF-8";
    public final Long MER_ID = 1643004211569439L;
    public final String MER_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMhyR4I/yTdDrrI6C7HhHEH20HtbkF+wrFWoG7Rk18TatN7L6rLcW3mV3fPZUM07W7pKEGVz01grrl/OoKHbKWAEm7n+WdRXsBJ+VinVSdI0IynquDjh6qTUM8gBrPaU3KpwSdVqdY51foQw4mr7h93Bq8W6GOkgBiosQYd2yYF5AgMBAAECgYEAk6ZyfEQmIPpPLePKB45tdT2slqF8+K+4UiRRvBDoS45a2x7UVEoza510cBItDfBighfaehDWpkUO3m3jLza0A0PHmbryNsyrw0HSLiPJVaf1/wP2jvI94veXIlL1ttyKsq1kSVhgUuRS2anq/IXdaF2G1NE/rmeGatCPg0kcp7ECQQDinjgD1z8D0y+7duL8HD3+sCL94VXlGrj2DrzI75zXyuf4CBqSSRSX1lfy3HHjw5alVnmUqVQPyIK/P50JWJxlAkEA4m9iy1czgowK7DTUWOGXMVobzZdjvS168FllIJxrj3dhAvoT6MduotUoZ3e+2e9F3AoGiLGh4+NM7lmciNCthQJAb33uCx2z9/rHyhA0nlp6uboIjSV2r+/UveIESUtxu80tER9Ti1L/GbCvtmF0et04WYHQ8S3ZmKZKhTMT3IxG7QJAMoRBGTZaIozVvv7GRRIHJhaBT2uv77ueytJW0mCzSm2KshxSpsMfdqpxeivn4z0Ta9S8wnBGus2yExbg6lEL9QJBAJG8lqFuPfpzHtoCd7Xbv0GWJ+K/modlWPWbxANJQw9OL2sqQ2Was/Ipflyggu+gDDkkhePe77YkSdlAJxVNbzY=";
    public final String MER_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIckeCP8k3Q66yOgux4RxB9tB7W5BfsKxVqBu0ZNfE2rTey+qy3Ft5ld3z2VDNO1u6ShBlc9NYK65fzqCh2ylgBJu5/lnUV7ASflYp1UnSNCMp6rg44eqk1DPIAaz2lNyqcEnVanWOdX6EMOJq+4fdwavFuhjpIAYqLEGHdsmBeQIDAQAB";
    public final String INTER_KEY = "930A7CE165CB2D27F62A34862A397618";
    public final String API_VERSION = "V1.0";
    public final String OMS_TEST_URL = "http://192.168.40.85:8907/clientapi/clientBusiness/common";

    //商户
    private static Merchant mer = new Merchant();
    //公共参数
    public static RequestMessage rm = new RequestMessage();
    //签约者
    public static Person p = new Person();
    //业务参数
    public static UserSignReqModel proSign = null;

    public  String runTestUserSignService() throws Exception {

        //商户
        //mer = new Merchant();
        // 商户id
        mer.setMerid(MER_ID);
        // 商户私钥
        mer.setPriKey(MER_PRIVATE_KEY);
        // 商户公钥
        mer.setPubKey(MER_PUBLIC_KEY);
        // 商户签名
        mer.setInterKey(INTER_KEY);


        //公共参数
        //RequestMessage rm = new RequestMessage();
        rm.setFunCode("6010");
        rm.setMerId(mer.getMerid());
        rm.setVersion(API_VERSION);
        rm.setReqId(rm.getFunCode() + System.currentTimeMillis());

        //签约者
        //Person p = new Person();
        //收款人
        p.setName(new RandInfo().randFamilyName());
        //收款卡号
        p.setPayAcc("6214830114752289");
        //收款身份证号
        p.setIdcard(new IDCard().getIDCard());
        //手机号
        p.setMobile("13167558700");

        //把person添加到自由职业者签约的参数中
        proSign = new UserSignReqModel(p.getName()
                , p.getPayAcc()
                , p.getMobile()
                , 0
                , 0
                , 30481
                , p.getIdcard()
        );

        //设置照片
        String filePathOn = "http://192.168.40.84:8888/group1/M00/02/7D/wKgoVGPtmICAHSWtAAl0j6fMl28379.png";
        String filePathBack = "http://192.168.40.84:8888/group1/M00/02/7E/wKgoVWPtmIWAJGF3AAn2SK9R8aw444.png";
        proSign.setIdCardPic1(filePathOn);
        proSign.setIdCardPic2(filePathBack);

        //reqData转json
        String reqData = JsonUtils.toJson(proSign);
        //System.out.println("reqDatajson= " + reqData);

        //reqData加密
        byte[] bs = DESUtils.encrypt(reqData.getBytes(CHARSET_UTF_8), mer.getInterKey());
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, mer.getPriKey()));

        String reqStr = JsonUtils.toJson(rm);
        //System.out.println("请求数据：" + reqStr);


        String resData = HttpClientHelper.sendPost(OMS_TEST_URL, "application/json", reqStr, "POST", CHARSET_UTF_8);
        //System.out.println("响应数据：" + resData);
        return resData;
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult();
        String resData = "";
        try {
            sampleResult.sampleStart();
            resData = runTestUserSignService();

            //断言
            JSONObject jsonObject1 = JSONObject.parseObject(resData);
            if (jsonObject1.get("resCode").equals("0000")) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true+"", SampleResult.TEXT);
                System.out.println("签约结果:" + true);
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + " " + jsonObject1.get("resMsg"), SampleResult.TEXT);
                System.out.println("签约结果:" + false + " " + jsonObject1.get("resMsg"));
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(resData, SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());
        } finally {
            sampleResult.sampleEnd();
        }

        return sampleResult;
    }
}
